/**
 * 作者:hanbo
 * 日期:2018/12/2610:34
 * 描述:
 */

var map;
var view;
var homeLayer;
require([
    "dojo/parser",
    "esri/Map",
    "esri/views/MapView",
    "esri/layers/ImageryLayer",
    "esri/layers/FeatureLayer",
    "esri/geometry/Extent"
    , "myModule/UIControl"
    , "dojo/domReady!"
], function (parser, Map, MapView, ImageryLayer, FeatureLayer, Extent
    , UIControl
) {
    parser.parse();

    var image = new ImageryLayer({
        url: "http://localhost:6080/arcgis/rest/services/home_DBO_baseImage/ImageServer",
        format: "jpgpng", // 影像服务
        title: "影像"
    });
    var langUse = new FeatureLayer({
        url: "http://localhost:6080/arcgis/rest/services/newHome/MapServer/1",
        title: "土地利用现状",
        visible: false,
        outFields: ["*"],
        render: {
            type: "unique-value",
            field: "DLBM",
            defaultSymbol: {type: "simple-fill"},  //默认符号
            uniqueValueInfos: [{
                value: 31,
                symbol: {
                    type: "simple-fill",
                    color: "green"
                }
            }, {
                value: 13,
                symbol: {
                    type: "simple-fill",
                    color: "yellow"
                }
            }, {
                value: "203",
                symbol: {
                    type: "simple-fill",
                    color: "red"
                }
            }, {

                value: "011",
                symbol: {
                    type: "simple-fill",
                    color: "blue"
                }
            }]
        },
        popupTemplate: {
            title: "土地利用现状{OBJECTID}",
            content: [{
                type: "fields",
                fieldInfos: [{
                    fieldName: "ZLDWMC",
                    label: "所属单位",
                    visible: true
                }, {
                    fieldName: "DLMC",
                    label: "用地类型",
                    visible: true
                }, {
                    fieldName: "DLBM",
                    label: "地类号",
                    visible: true
                }, {
                    fieldName: "TBBH",
                    label: "图斑号",
                    visible: true
                }]
            }]
        }
    });
    homeLayer = new FeatureLayer({
        url: "http://localhost:6080/arcgis/rest/services/newHome/MapServer/0",
        title: "宅基地",
        outFields: ["*"],
        popupTemplate: {
            title: "宅基地{id}",
            content: [{
                type: "fields",
                fieldInfos: [{
                    fieldName: "PEOPLE",
                    label: "权属人",
                    visible: true
                }, {
                    fieldName: "DLMC",
                    label: "用地类型",
                    visible: true
                }, {
                    fieldName: "DLBM",
                    label: "地类号",
                    visible: true
                }, {
                    fieldName: "TBBH",
                    label: "图斑号",
                    visible: true
                },
                    {
                        fieldName: "MJ",
                        label: "面积（平方米）",
                        visible: true
                    },
                    {
                        fieldName: "TIME",
                        label: "登记时间",
                        visible: true
                    },
                    {
                        fieldName: "QSDWMC",
                        label: "所属村组",
                        visible: true
                    }
                ]
            }]
        }
    });
    map = new Map({
        basemap: "osm",
        layers: [image,langUse, homeLayer]
    });


    view = new MapView({
        container: "map",
        map: map
    });

    view.when(function () {
        var uiControl = new UIControl(view);
        uiControl.setScaleBar(true);

        $("#print").click(function () {
            var state=$('#print i');
            if(state .hasClass('checked')){
                state.removeClass('checked');
                uiControl.openPrint(false);
            }else {
                state.addClass('checked');
                uiControl.openPrint(true);
            }
        });
        //设置地图范围
        view.extent = new Extent({
            xmin: 103.300136976,
            ymin: 30.3538426998,
            xmax: 103.342034525,
            ymax: 30.3811850862,
            spatialReference: {
                wkid: 4326
            }
        });
        //
        // var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
        // saveAs(blob, "hello world.txt");
    });
});

function setBaseMap(node, type) {
    $("#baseMapDiv li").removeClass("choose");
    $(node).addClass("choose");
    switch (type) {
        case "1":
            setLineMap("http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}");
            break;
        case "2":
            setLineMap(" http://www.google.cn/maps/vt?lyrs=y@189&gl=cn&x={x}&y={y}&z={z}");
            break;
        case "3":
            setLineMap(" http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}");
            break;
        case "4":
            setLineMap(" http://www.google.cn/maps/vt?lyrs=p@189&gl=cn&x={x}&y={y}&z={z}");
            break;
        case "5":
            setLineMap("http://webst01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}");
            break;
        default:
            map.basemap = type;
            break
    }
}

function setLineMap(url) {
    require(["esri/Basemap", "esri/layers/WebTileLayer", "esri/layers/support/TileInfo", "myModule/GoogleMapLayer", "dojo/domReady!"], function (Basemap, WebTileLayer, TileInfo, GoogleMapLayer) {


        var basemap = new Basemap({
            baseLayers: [
                new GoogleMapLayer({urlTemplate: url})
            ]
            // ,
            //     referenceLayers: [
            //         new WebTileLayer()
            //     ]
        });
        map.basemap = basemap;
    })
}